/**
 *
 *  加载UI，可以在初始化的时候就载入
 *
 */
class LoadingView extends BaseEuiView {
    public loadingRun: eui.Image;
    public loadingText: eui.Label;
    public loadingBg: eui.Image;
    public loadingRunPATH: string = Resource.getAssets() + "Load/loading.png";
    public loadingBgPATH: string;

    public constructor() {
        super();
        this.left = this.right = this.bottom = this.top = 0;
    }
    protected initUI(): void {
        this.getAsset(this.loadingRunPATH,this.loadingComplete,this);
    }

    public setBgRes(source: string): void {
        this.loadingBgPATH = source;
        this.getAsset(source,this.loadingComplete,this);
    }


    private loadingComplete(texture: egret.Texture,source: string) {
        if(source == this.loadingBgPATH) {
            this.loadingBg.source = texture;
        }
        else if(source == this.loadingRunPATH) {
            this.loadingText.text = "0%";
            this.loadingRun.source = texture;
            this.setAnchor(this.loadingRun,this.loadingRun.width * 0.5,this.loadingRun.height * 0.5);
            this.loadingRun.x = StageScene.get.getWidth() / 2;
            this.loadingRun.y = StageScene.get.getHeight() / 2;

            this.addEventListener(egret.Event.REMOVED_FROM_STAGE,() => {
                if(this.hasEventListener(egret.Event.ENTER_FRAME)) {                    this.removeEventListener(egret.Event.ENTER_FRAME,this.runLoading,this);                }            },this);
            this.addEventListener(egret.Event.ENTER_FRAME,this.runLoading,this);
        }
    }


    public setProgress(itemsLoaded: number,itemsTotal: number): void {        if(this.loadingText) {
            this.loadingText.text = Math.round(itemsLoaded / itemsTotal * 100) + "%";
        }    }

    private runLoading(evt: egret.Event) {
        Log.d("loadingRun x:" + this.loadingRun.x + ",y:" + this.loadingRun.y);
        this.loadingRun.rotation += 3;    }

    protected exml(): string {
        return Resource.getDefaultExmlDir() + "LoadingSkin.exml";
    }

}
